package visual

import (
	"cgs-server/server/base"
	"cgs-server/server/model"
	"errors"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo"
)

type ConfigRepository interface {
	List() ([]*Config, error)
	Add(visual *Config, collectionName string, user *model.User) (*mongo.InsertOneResult, error)
	base.Repository
	FindById(visualId primitive.ObjectID) (*Config, error)
}

func NewConfigRepository() ConfigRepository {
	return &configRepository{}
}

// visual
type configRepository struct {
	base.RepositoryStruct
}

func (c *configRepository) List() ([]*Config, error) {
	return nil, nil
}

func (c *configRepository) Add(config *Config, collectionName string, user *model.User) (*mongo.InsertOneResult, error) {
	if err := c.CreateDB(collectionName); err != nil {
		return nil, err
	}
	config.ResolveEntity(config, user)
	/*configDoc := bson.M{
		"ID":        config.IdObjID,
		"VisualId":  config.VisualId,
		"Detail":    config.Detail,
		"Component": config.Component,
		"SceneUrl": config.SceneUrl,
		"UserID": config.UserId,
	}*/
	return c.Mongo.InsertOne(c.CollectionName, config)
}

func (c *configRepository) FindById(visualId primitive.ObjectID) (*Config, error) {
	filter := bson.M{
		"VisualId": visualId,
	}
	result := &Config{}
	find, err := c.Mongo.FindOne(c.CollectionName, filter, result)
	if err != nil {
		return nil, err
	}
	if find {
		return result, nil
	}
	return nil, err
}

func (c *configRepository) Update(mdl *model.DataUserModel) error {
	if visualModel, ok := mdl.Data.(*Model); ok {
		if err := c.CheckDB(); err != nil {
			return err
		}

		config := &visualModel.Config
		filter := bson.M{
			"ID": config.ID,
		}
		if config.ID.IsZero() {
			filter = bson.M{
				"VisualId": visualModel.Visual.ID,
			}
		}

		config.ResolveFilter(config, mdl.User, &filter)

		updateOpt := bson.M{}
		if config.Detail != "" {
			updateOpt["Detail"] = config.Detail
		}
		if config.Component != "" {
			updateOpt["Component"] = config.Component
		}
		if config.SceneUrl != "" {
			updateOpt["SceneUrl"] = config.SceneUrl
		}

		if config.RequestData != "" {
			updateOpt["requestData"] = config.RequestData
		}

		if config.AutoTrigger != nil {
			updateOpt["autoTrigger"] = config.AutoTrigger
		}

		config.ResolveUpdateCondition(config, mdl.User, &updateOpt)
		update := bson.M{
			"$set": updateOpt,
		}
		_, err := c.Mongo.UpdateOne(c.CollectionName, filter, update)
		return err
	} else {
		return errors.New("VisualModel type error")
	}
}

// 删除视图配置
func (c *configRepository) Delete(mdl *model.DataUserModel) error {
	if visualModel, ok := mdl.Data.(*Model); ok {
		if err := c.CheckDB(); err != nil {
			return err
		}

		config := &visualModel.Config
		filter := bson.M{
			"VisualId": visualModel.Visual.ID,
		}
		config.ResolveFilter(config, mdl.User, &filter)
		_, err := c.Mongo.DeleteOne(c.CollectionName, filter)
		return err
	} else {
		return errors.New("VisualModel type error")
	}
}

// 配置详情
func (c *configRepository) FindOne(mdl *model.DataUserModel) (interface{}, error) {
	if visualModel, ok := mdl.Data.(*Model); ok {
		if err := c.CheckDB(); err != nil {
			return nil, err
		}
		config := &visualModel.Config
		filter := bson.M{
			"VisualId": visualModel.Visual.ID,
		}
		config.ResolveFilter(config, mdl.User, &filter)
		result := &Config{}
		find, err := c.Mongo.FindOne(c.CollectionName, filter, result)
		if err != nil {
			return nil, err
		}
		if !find {
			return nil, errors.New("config not found")
		}
		return result, nil
	} else {
		return nil, errors.New("VisualModel type error")
	}
}
